﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameBase
{
    public abstract class My2DSprite : VisibleGameEntity
    {
        
        protected Texture2D[] _lstTexture;

        public Texture2D[] LstTexture
        {
            get { return _lstTexture; }
            set
            {
                _lstTexture = value;
                _nTexture = _lstTexture.Length;
                _iTexture = 0;
            }
        }
        protected int _nTexture;

        public int nTexture
        {
            get { return _nTexture; }
            set { _nTexture = value; }
        }
        protected int _iTexture;

        public int iTexture
        {
            get { return _iTexture; }
            set { _iTexture = value; }
        }

        protected float _nomalDelay = 16;

        public float NomalDelay
        {
            get { return _nomalDelay; }
            set { _nomalDelay = value; }
        }

        public bool IsRight
        {
            get { return _isRight; }
            set { _isRight = value; }
        }

        public override void draw(GameTime gameTime, SpriteBatch spriteBatch, Color color)
        {
            if(IsRight)
                spriteBatch.Draw(LstTexture[iTexture], Position, null, color, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
            else
                spriteBatch.Draw(LstTexture[iTexture], Position, null, color, 0, Vector2.Zero, Scale, SpriteEffects.FlipHorizontally, 0);
        }

        public bool isClicked(float x, float y)
        {
            if (x >= Position.X && x <= Position.X + Size.X
                || y >= Position.Y && y <= Position.Y + Size.Y)
            {
                return true;
            }
            return false;
        }

        public My2DSprite(string imgStr, int nTexture, Vector2 pos, float scale, float delay)
        {
            Texture2D texture = Constant.content.Load<Texture2D>(imgStr);
            internalInit(nTexture, pos, scale, delay, texture);
        }

        public My2DSprite(Texture2D texture, int nTexture, Vector2 pos, float scale, float delay)
        {
            internalInit(nTexture, pos, scale, delay, texture);
        }

        private void internalInit(int nTexture, Vector2 pos, float scale, float delay, Texture2D texture)
        {
            LstTexture = new Texture2D[nTexture];
            _nTexture = nTexture;
            int width = texture.Width / nTexture;
            int height = texture.Height;
            Scale = scale;

            Size = scale * new Vector2(width, height);

            for (int i = 0; i < nTexture; i++)
            {
                LstTexture[i] = Texture2DHelper.createPartImage(Constant.graphics, new Rectangle(i * width, 0, width, height), texture);
            }
            Position = pos;
            _nomalDelay = delay;
        }

        public virtual  bool isEndOfFrame()
        {
            return iTexture == nTexture - 1;      
        }

        public virtual bool isBeginOfFrame()
        {
            return iTexture == 0;           
        }

        public void resetFrame()
        {
            iTexture = 0;
        }
    }
}
